﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_frmAdTaoCH : System.Web.UI.Page
{
    DataClassesDataContext db = new DataClassesDataContext();
    clsCauHoi frCH = new clsCauHoi();
    protected void Page_Load(object sender, EventArgs e)
    {
        btnThemCH.Attributes.Add("onclick", "return ktRadioButton();");
        if (!Page.IsPostBack)
        {
            if (db.tbl_NGANHANGCAUHOIs.Count() != 0)
            {
                this.GridView1.SelectedIndex = 0;
                BindGrvToText();
            }


        }
    }

    public void BindGrvToText()
    {
        //txtMaCH.Text = GridView1.SelectedRow.Cells[1].Text;
        txtNoidungCH.Text = GridView1.SelectedRow.Cells[2].Text;
        drplMucdo.SelectedItem.Text = GridView1.SelectedRow.Cells[8].Text;
        txtA.Text = GridView1.SelectedRow.Cells[3].Text;
        txtB.Text = GridView1.SelectedRow.Cells[4].Text;
        txtC.Text = GridView1.SelectedRow.Cells[5].Text;
        txtD.Text = GridView1.SelectedRow.Cells[6].Text;
        //drplDAdung.SelectedItem.Text = GridView1.SelectedRow.Cells[7].Text;

    }
    public void Hamdong()
    {
       // txtMaCH.Enabled = false;
        txtNoidungCH.Enabled = false;
        txtA.Enabled = false;
        txtB.Enabled = false;
        txtC.Enabled = false;
        txtD.Enabled = false;
       // drplDAdung.Enabled = false;
        drplMucdo.Enabled = false;

    }

    public void Trangthaibd()
    {
        btnThemCH.Text = "Them CH";
        btnSua.Text = "Sua CH";
        btnHuy.Text = "Huy Thao Tac";
        btnThemCH.Enabled = true;
        btnXoaCH.Enabled = true;
        btnSua.Enabled = true;

        Hamdong();

    }
    protected void btnThemCH_Click(object sender, EventArgs e)
    {
        if (btnThemCH.Text == "Them CH")
        {
            //txtMaCH.Enabled = true;
            txtNoidungCH.Enabled = true;
            txtA.Enabled = true;
            txtA.Text = null;
            txtB.Enabled = true;
            txtB.Text = null;
            txtC.Enabled = true;
            txtC.Text = null;
            txtD.Enabled = true;
            txtD.Text = null;
            //drplDAdung.Enabled = true;
            //drplDAdung.Items.Clear();
            drplMucdo.Enabled = true;
            btnThemCH.Text = "Luu CH";
            //btnXoaCH.Enabled = false;
            //btnSua.Enabled = false;
        }
        else if (btnThemCH.Text == "Luu CH")
        {

            //string mach = txtMaCH.Text;
            string noidungch = txtNoidungCH.Text;
            string da1 = txtA.Text;
            string da2 = txtB.Text;
            string da3 = txtC.Text;
            string da4 = txtD.Text;
            string dokho = drplMucdo.Text;

            List<RadioButton> check = new List<RadioButton>();
            check.Add(rdbA);
            check.Add(rdbB);
            check.Add(rdbC);
            check.Add(rdbD);
            List<string> da = new List<string>();
            da.Add(da1);
            da.Add(da2);
            da.Add(da3);
            da.Add(da4);
            string dapan=string.Empty ;
            for (int i = 0; i < 4; i++)
            {
                if (check[i].Checked)
                {
                    dapan = da[i];
                    break;
                }
                
            }
                
           
           
            frCH.ThemCHmoi(noidungch, da1, da2, da3, da4, dapan, dokho);
            GridView1.DataBind();
            //rfvMaCH.ControlToValidate = txtMaCH.ID;
            //rfvnoidung.ControlToValidate = txtNoidungCH.Text;
            // rfvDaA.ControlToValidate = txtA.Text;



            btnThemCH.Text = "Them CH";
            Trangthaibd();

        }
    }

    protected void btnXoaCH_Click(object sender, EventArgs e)
    {
        try
        {
            string machf = GridView1.SelectedRow.Cells[1].Text;
            frCH.XoaCH(machf);
            GridView1.DataBind();
            BindGrvToText();
        }
        catch
        {

        }
    }



    protected void btnSua_Click(object sender, EventArgs e)
    {
        if (btnSua.Text == "Sua CH")
        {
            //txtMaCH.Enabled = true;
            txtNoidungCH.Enabled = true;


            txtA.Enabled = true;
            txtB.Enabled = true;
            txtC.Enabled = true;
            txtD.Enabled = true;
            //drplDAdung.Enabled = true;
            //drplDAdung.Items.Clear();
            drplMucdo.Enabled = true;

            //drplDAdung.Items.Add(txtA.Text);
            //drplDAdung.Items.Add(txtB.Text);
            //drplDAdung.Items.Add(txtC.Text);
            //drplDAdung.Items.Add(txtD.Text);



            btnSua.Text = "Luu CH";
            btnThemCH.Enabled = false;
            btnXoaCH.Enabled = false;
        }
        else if (btnSua.Text == "Luu CH")
        {

           // string mach = txtMaCH.Text;
            string noidungch = txtNoidungCH.Text;
            string da1 = txtA.Text;
            string da2 = txtB.Text;
            string da3 = txtC.Text;
            string da4 = txtD.Text;
            string dokho = drplMucdo.Text;
           // string dapan = drplDAdung.SelectedItem.Text;
            //frCH.SuaCH( noidungch, da1, da2, da3, da4, dapan, dokho);
            GridView1.DataBind();
            btnSua.Text = "Sua CH";
            Trangthaibd();

        }
    }
    //public void ThemDAvaodrl(string nd)
    //{
    //    drplDAdung.Items.Add(nd);
    //    drplDAdung.DataBind();
    //}

    protected void btnHuy_Click(object sender, EventArgs e)
    {
        Trangthaibd();
    }

    protected void txtA_TextChanged(object sender, EventArgs e)
    {
        string nda = txtA.Text;
        if (btnSua.Text == "Luu CH")
        {

            //drplDAdung.Items[0].Text = txtA.Text;
        }
        if (btnThemCH.Text == "Luu CH")
        {
            //ThemDAvaodrl(nda);
        }
        txtB.Focus();
    }

    protected void txtB_TextChanged(object sender, EventArgs e)
    {
        string nda = txtB.Text;
        if (btnSua.Text == "Luu CH")
        {

            //drplDAdung.Items[1].Text = txtB.Text;
        }
        if (btnThemCH.Text == "Luu CH")
        {
            //ThemDAvaodrl(nda);
        }
    }

    protected void txtC_TextChanged(object sender, EventArgs e)
    {
        string nda = txtC.Text;
        if (btnSua.Text == "Luu CH")
        {

            //drplDAdung.Items[2].Text = txtC.Text;
        }
        if (btnThemCH.Text == "Luu CH")
        {
            //ThemDAvaodrl(nda);
        }
    }

    protected void txtD_TextChanged(object sender, EventArgs e)
    {
        string nda = txtD.Text;
        if (btnSua.Text == "Luu CH")
        {

            //drplDAdung.Items[3].Text = txtD.Text;
        }
        if (btnThemCH.Text == "Luu CH")
        {
           // ThemDAvaodrl(nda);
        }
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        //txtMaCH.Text = GridView1.SelectedRow.Cells[1].Text;
        txtNoidungCH.Text = GridView1.SelectedRow.Cells[2].Text;
        
        txtA.Text = GridView1.SelectedRow.Cells[3].Text;
        txtB.Text = GridView1.SelectedRow.Cells[4].Text;
        txtC.Text = GridView1.SelectedRow.Cells[5].Text;
        txtD.Text = GridView1.SelectedRow.Cells[6].Text;

        //if (drplDAdung.Items.Count != 0)
        //{
        //    drplDAdung.SelectedItem.Text = GridView1.SelectedRow.Cells[7].Text;
        //}
        //else
        //{
        //    drplDAdung.Items.Add(new ListItem());
        //    drplDAdung.SelectedItem.Text = GridView1.SelectedRow.Cells[7].Text;
        //}

        Trangthaibd();
    }
}
